﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PioneerBoilerplate.Model.Admin;
using PioneerBoilerplate.Repository;
using PioneerBoilerplate.Repository.Repositories;
using PioneerBoilerplate.Repository.UnitOfWorks;
using System.Linq;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace PioneerBoilerplate.Api.Controllers.Admin
{
    public class TestController : AreaController
    {
        private readonly IUnitOfWorkManager _unitOfWorkManager;

        private readonly IUserRoleRepository _userRoleRepository;
        private readonly IRoleRepository _roleRepository;
        private readonly IUserRepository _userRepository;

        private readonly IPermissionRepository _permissionRepository;
        private readonly IRolePermissionRepository _rolePermissionRepository;

        public TestController(
            IUnitOfWorkManager unitOfWorkManager,
        IUserRoleRepository userRoleRepository,
            IRoleRepository roleRepository,
            IUserRepository userRepository,
            IPermissionRepository permissionRepository,
            IRolePermissionRepository rolePermissionRepository)
        {
            _unitOfWorkManager = unitOfWorkManager;
            _userRoleRepository = userRoleRepository;
            _roleRepository = roleRepository;
            _userRepository = userRepository;
            _permissionRepository = permissionRepository;
            _rolePermissionRepository = rolePermissionRepository;

        }

        /// <summary>
        /// 用户角色关联
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task Test()
        {
            using (var uowHandle = _unitOfWorkManager.Begin<ApplicationDbContext>())
            {

                var users = await _userRepository.GetAllListAsync();
                var roles = await _roleRepository.GetAllListAsync();

                foreach (var user in users)
                {
                    foreach (var role in roles)
                    {
                        await _userRoleRepository.InsertAsync(new UserRoleEntity { Id = YitIdHelper.NextId(), RoleId = role.Id, UserId = user.Id });
                    }
                }

                uowHandle.SaveChange();
            }
        }

        /// <summary>
        /// 用户角色关联
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task Test2()
        {
            using (var uowHandle = _unitOfWorkManager.Begin<ApplicationDbContext>())
            {

                var permissions = await _permissionRepository.GetAllListAsync();
                var roles = await _roleRepository.GetAllListAsync();

                foreach (var permission in permissions)
                {
                    foreach (var role in roles)
                    {
                        await _rolePermissionRepository.InsertAsync(new RolePermissionEntity { Id = YitIdHelper.NextId(), RoleId = role.Id, PermissionId = permission.Id });
                    }
                }

                uowHandle.SaveChange();
            }
        }
    }
}
